<?php

namespace App\Http\Controllers\Admin\Ad;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\CommonController;
use App\Models\wechat\Ad;
use App\Models\wechat\ad_category;

class AdController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_system'=>'ads',
            '_ad'=>'am-active',

        ]);
    }

    //显示广告列表
    public function index(Request $request)
    {
        //多条件查找
        $where=function ($query) use ($request){
            if($request->has('category_id')&&$request->category_id!='-1'){
                $query->where('category_id',$request->category_id);
            }
        };

        $ads=Ad::with('category')->where($where)->
            orderBy('sort_order')->
        paginate(config('fangshop.page_size'));
//        return $ads;
        return view('admin.ads.ad.index')->with('ads',$ads);
    }

    //新增
    public function create()
    {
        $categories=ad_category::get();
        return view('admin.ads.ad.create')->with('categories',$categories)->with(['_createAd'=>'am-active','_ad'=>'']);
    }

    //存储
    public function store(Request $request)
    {
        Ad::create($request->all()); //把所有提交上来的数据添加到数据库
        return redirect(route('ads.ad.index'))->with('ads','新增广告成功');
    }

    //编辑
    public function edit($id)
    {
        $ad=Ad::find($id);
        $categories=ad_category::get();
//        return $ad;
        return view('admin.ads.ad.edit')->with('categories',$categories)->with('ad',$ad);
    }

    //更新
    public function update(Request $request,$id)
    {
        $ad=Ad::find($id);
        $ad->update($request->all()); //修改request上提交的所有数据
        return redirect(route('ads.ad.index'))->with('ads','修改广告成功');
    }

    //删除
    public function destroy($id)
    {
        Ad::destroy($id);
        return redirect(route('ads.ad.index'))->with('ads','删除广告成功');
    }
    //多选删除
    public function destroy_checked(Request $request)
    {
        $ids=$request->ids; //$ids是个字符串
        $ids=explode(' ',trim($ids)); //ids是个数组
        foreach ($ids as $id){
            Ad::destroy($id);
        }

    }


    //排序
    public function sort_order(Request $request)
    {
        $ad=Ad::find($request->id);
        $ad->sort_order=$request->sort_order;
        $ad->save();
    }



    //显示广告回收站
    public function trash()
    {
        $ads=Ad::onlyTrashed()  // withTrashed()
        ->orderBy('deleted_at', "desc")->paginate(config('fangshop.page_size'));
//        return $ads;
        return view('admin.ads.ad.trash')->with('ads',$ads)->with(['_trash'=>'am-active','_ad'=>'']);
    }

    //回收站恢复单个
    public function restore($id)
    {
        Ad::withTrashed()->where('id', $id)->restore();
        return back();
    }
    //回收站强制删除
    public function delete_force($id)
    {
        $ad=Ad::withTrashed()->find($id);
        $ad->forceDelete();
        return back();
    }

    //回收站恢复多个restore_checked
    public function restore_checked(Request $request)
    {
        $ids=$request->ids; //$ids是个字符串
        $ids=explode(' ',trim($ids));
        foreach ($ids as $id){
            Ad::withTrashed()->where('id', $id)->restore();
        }
//        return back(); 不用return back() ajax的success方法里自动刷新界面
    }
}
